/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guide.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guide.entity.Guiding;
import com.thinkgem.jeesite.modules.guide.service.GuidingService;

/**
 * 引导灯配置Controller
 * @author xlp
 * @version 2019-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/guide/guiding")
public class GuidingController extends BaseController {

	@Autowired
	private GuidingService guidingService;
	
	@ModelAttribute
	public Guiding get(@RequestParam(required=false) String id) {
		Guiding entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = guidingService.get(id);
		}
		if (entity == null){
			entity = new Guiding();
		}
		return entity;
	}
	
	@RequiresPermissions("guide:guiding:view")
	@RequestMapping(value = {"list", ""})
	public String list(Guiding guiding, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Guiding> page = guidingService.findPage(new Page<Guiding>(request, response), guiding); 
		model.addAttribute("page", page);
		return "modules/guide/guidingList";
	}

	@RequiresPermissions("guide:guiding:view")
	@RequestMapping(value = "form")
	public String form(Guiding guiding, Model model) {
		model.addAttribute("guiding", guiding);
		return "modules/guide/guidingForm";
	}

	@RequiresPermissions("guide:guiding:edit")
	@RequestMapping(value = "save")
	public String save(Guiding guiding, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, guiding)){
			return form(guiding, model);
		}
		guidingService.save(guiding);
		addMessage(redirectAttributes, "保存引导灯配置成功");
		return "redirect:"+Global.getAdminPath()+"/guide/guiding/?repage";
	}
	
	@RequiresPermissions("guide:guiding:edit")
	@RequestMapping(value = "delete")
	public String delete(Guiding guiding, RedirectAttributes redirectAttributes) {
		guidingService.delete(guiding);
		addMessage(redirectAttributes, "删除引导灯配置成功");
		return "redirect:"+Global.getAdminPath()+"/guide/guiding/?repage";
	}

}